package com.zaizai.handler;

import com.zaizai.domain.AjaxResult;
import com.zaizai.exception.GlobalException;
import com.zaizai.exception.LoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局Runtime异常处理器
 */
@Slf4j
@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public AjaxResult exception(Exception e) {
        e.printStackTrace();
        return AjaxResult.error(500, "系统异常");
    }

    @ExceptionHandler(value = GlobalException.class)
    public AjaxResult globalException(GlobalException e) {
        e.printStackTrace();
        return AjaxResult.error(500, e.getMsg());
    }

    @ExceptionHandler(value = LoginException.class)
    public AjaxResult loginException(LoginException e) {
        e.printStackTrace();
        return AjaxResult.error(401, e.getMsg());
    }
    @ExceptionHandler(value = RedisConnectionFailureException.class)
    public AjaxResult redisException(RedisConnectionFailureException e) {
        log.info("Redis连接异常  + {}", e.getMessage());
        return AjaxResult.error(500, "Redis连接异常");
    }
}
